#include "monitor.h"
#include "globalconst.h"

Monitor* Monitor::instance = 0;

Monitor::Monitor(QObject *parent) :
        QObject(parent) , DDR(GlobalConst::EMPTY_REGISTER), DSR(GlobalConst::EMPTY_REGISTER)
{
    DSR.replace(31, 1, "1");
}

Monitor::~Monitor()
{
}

Monitor* Monitor::getInstance(QObject *parent)
{
    if(instance == 0)
    {
        instance = new Monitor(parent);
    }
    return instance;
}

QString Monitor::getDSR(int partIndex)
{
    return DSR.mid(8 * partIndex, 8);
}

void Monitor::setDDR(QString input, int partIndex)
{
    Q_ASSERT(input.size() == 8);
    DDR.replace(8 * partIndex, 8, input);
    if(partIndex == 3)
    {
        bool ok;
        int letter = DDR.mid(24).toInt(&ok, 2);
        DSR.replace(31, 1, "0");
        emit letterAvailable(QChar(letter));
    }
}

void Monitor::setDSR()
{
    DSR.replace(31, 1, "1");
}
